//
//  PopEditPopAnimator.swift
//  CMDB
//
//  Created by 曹桂祥 on 2017/5/18.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

class PopEditPopAnimator: NSObject {
    
    //对外提供的属性
    var isPresented : Bool = false
    
    var presentedFrame : CGRect = CGRectZero
    
    var callBack : ((presented : Bool)->())?
    
    init(callBack:(presented : Bool) -> ()) {
        self.callBack = callBack
    }

}


extension PopEditPopAnimator : UIViewControllerTransitioningDelegate{
    
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        
        let presentation = CMDBPresentationController(presentedViewController: presented, presentingViewController: presenting)
        
        presentation.presentedFrame = presentedFrame
        
        return presentation
    }
    
    //目的：自定义弹出动画
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented = true
        
        callBack!(presented : isPresented)
        
        return self
    }
    
    // 目的:自定义消失的动画
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented = false
        callBack!(presented : isPresented)
        
        return self
    }
    
}

// MARK:- 弹出和消失动画代理的方法
extension PopEditPopAnimator : UIViewControllerAnimatedTransitioning{
    
    // 动画执行的时间
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.375
    }
    
    /// 获取`转场的上下文`:可以通过转场上下文获取弹出的View和消失的View
    // UITransitionContextFromViewKey : 获取消失的View
    // UITransitionContextToViewKey : 获取弹出的View
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        isPresented ? animationForPresentedView(transitionContext) : animationForDismissedView(transitionContext)
    }
    
    /// 自定义弹出动画
    private func animationForPresentedView(transitionContext: UIViewControllerContextTransitioning) {
        // 1.获取弹出的View
        let presentedView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        
        presentedView.layer.masksToBounds = true
        
        presentedView.layer.cornerRadius  = 5
        
        // 2.将弹出的View添加到containerView中
        transitionContext.containerView()?.addSubview(presentedView)
        
        // 3.执行动画
       
        presentedView.y                 = 1.35 * CMDB_Screen_Height
       
        presentedView.layer.anchorPoint = CGPointMake(0.5, 1)
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            //            presentedView.transform = CGAffineTransformIdentity
        
            presentedView.y                 = 0.35 * CMDB_Screen_Height
            
            
        }) { (_) -> Void in
            // 必须告诉转场上下文你已经完成动画
            transitionContext.completeTransition(true)
        }
    }
    
    
    /// 自定义消失动画
    
    
    
    private func animationForDismissedView(transitionContext: UIViewControllerContextTransitioning) {
        // 1.获取消失的View
        let dismissView = transitionContext.viewForKey(UITransitionContextFromViewKey)
        
        dismissView!.layer.masksToBounds = true
        
        dismissView!.layer.cornerRadius  = 5
        
        // 2.执行动画
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            
        dismissView!.y                 = 1.35 * CMDB_Screen_Height
           
            
        }) { (_) -> Void in
            dismissView?.removeFromSuperview()
            // 必须告诉转场上下文你已经完成动画
            transitionContext.completeTransition(true)
        }
    }

    
}